#!/usr/bin/env python
# coding=utf-8
import requests
#import sys
#print sys.path
#sys.path.append(r'/home/cc/dbc/mlcc')
#print sys.path
#from idea import getProxies
headers1 = {
    'accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
    'accept-encoding': "gzip, deflate, br",
    'accept-language': "zh-CN,zh;q=0.9,en;q=0.8",
    'cache-control': "no-cache",
    'cookie': "JSESSIONID=xiyYQTe-ytOYSz4F3F5TzfFUU5-6TzwfWQ-F7u; cna=2VFTEjKpJ3oCAQFQAHYwDMkL; ali_apache_tracktmp=\"c_w_signed=Y\"; _cn_slid_=gPrcZfT8sT; userIDNum=BUrMzAc6wfDTwSAuG2wWWw%3D%3D; ali_beacon_id=36.47.161.25.1511495723860.797245.0; ali_ab=113.140.248.28.1509185476182.6; ctoken=bUf5PGwsF03WqRCCHQHhnaga; enc=%2BMAFpHqZ65zQL35j89jbq5NuNTNkSKhW136KguIo04sPYCMi2U95sTRO6P2u00GSw694xj2LvporJoj6Q091hA%3D%3D; t=410a18151ec8d5b9ba336da770ed8b8d; lid=%E6%80%AA%E5%8F%AB%E4%B9%8B%E9%9F%B3; LoginUmid=7SsvVgWNTwPzzxteAX3yCWeSmeHeQ36%2FbYHrm9c%2FJSpTAhztWl1ziA%3D%3D; userID=Z7ChvN4XB2t3cREYTris%2BjEkXl5f8uDXhEEhL4t9Tks6sOlEpJKl9g%3D%3D; h_keys=\"%u6d74%u6876#%u8033%u673a%u82f9%u679c6#%u8033%u673a#%u7ae5%u88c5#%u60c5%u8da3#%u60c5%u8da3%u7528%u54c1#%u6d17%u9f3b%u5668#%u6df1%u5733%u9f99%u5c97%u533a#a#%u6df1%u5733%u9f99%u5c97%u533a+%u6210%u4eba%u7528%u54c1\"; ad_prefer=\"2018/10/09 11:40:01\"; _tb_token_=U8y9XfXNLDmjv3ATMg2C; UM_distinctid=166c7c12f39928-0c9002dac704a3-36664c08-1fa400-166c7c12f3a9eb; _tmp_ck_0=zbfvknZrLpstmYmvO7mS3mwdufkoHpglm9TiOegkbyYwkb%2BiY1%2BW%2FacGg8F%2F1QM5b%2BHK9Nm50IgUo2zzQekG4his9uucoSpb6zUFuuSjrWn0plpyl5r0AM1bzYWaEV%2FuNa7sXcqC25mjrftxJlmJLEHdp5Dg0kpeBR5FBwPxK5i9OPj7tQeXej6n4zc60vmfCAIOY2Dn6i9gi98Mq%2BzBWGr6vwU31RWFjRaU0npE2yr5AuuG2wsp4JzKJi4yQJqTWvzY%2Bm4nhq8L5GvoMIcc0u1nDXUxueGH4Q4u9bcdecUr37n5M8dcdNkOBb%2BodPesDX4SqB315OzcmNhkAurQCAd%2BlsKx2H67ZJKmYZ64%2BdLr2QaGqU%2FF3R9mH%2Fnw8XRzqOh%2F7%2B3KR5s2WI7GZduFi00UxodO57ZW5lLwK%2BsDu5F2BVvP9yd4qu5tSsS7bulkY%2FFTxYLKhRRZId1Y8gjQiN8gJctTYBzbyYXZdeak9CRxNheCC7K4kQuPJub9y9O%2BRrC3B%2FNLOfB46Uy0ZFIqbl3UZlI1IVnd1mJXFFn6lQMog9tChFQ2gQ%3D%3D; hng=CN%7Czh-CN%7CCNY%7C156; last_mid=b2b-2439417348; _csrf_token=1545623795539; cookie2=147fe1c621a672566ab80037743d905d; _m_h5_tk=42e294750b2134670211b58dab0530c3_1545649633579; _m_h5_tk_enc=5bf1b1b6b52fd9b852da1fa081283da7; cn_m_s=Fdzi2nhfocEn+50Sq92TYNCHbN+JBUtZ0U1dGnplvU77FdFtU+qx5Qo1PDezgqGh; cn_m_s.sig=Z4O2U76j1ML8tjXAmBfeosJ5EEWOzZj5ibqQZBl6DJE; ali_apache_track.sig=4wrCgK6P-jW7hSfDShyPDlchU-0Qnq0VjG-7Rr5zMFI; cookie2.sig=DpqJ32S7wVs2l_5xPevEXH161xIFxAeVlS75NoaSBng; tbsnid=fJlaYB88jdgRXMYbcA2LWYa0XGCY2B7G4p26aWsDK+g6sOlEpJKl9g==; tbsnid.sig=fYtnadN1hHyHuHVbrbj9Jp0JPq8jbGrTaRG5KqZUr_g; __cn_logon__.sig=_ajkY7FU0uQvAF-ntvenQBFbCtNdfk148wQYAOovzG0; unb.sig=B6ikIVDgwjsGioNWYDVrP-g30CbOdjk6Uu4tFa3txXs; __cn_logon_id__.sig=qAnLNzGWFzhFz9L1Jc4y_z38uUTzcOmZWfPIidSQbuU; ali-ss=eyJ1c2VySWQiOiIyNDM5NDE3MzQ4IiwibG9naW5JZCI6IuaAquWPq+S5i+mfsyIsInNpZCI6IjE0N2ZlMWM2MjFhNjcyNTY2YWI4MDAzNzc0M2Q5MDVkIiwiZWNvZGUiOiIiLCJtZW1iZXJJZCI6ImIyYi0yNDM5NDE3MzQ4Iiwic2VjcmV0IjoialNVb2RMNG56UzRFQjc3eDZGNmZST21MIiwibG9naW5TdGF0dXNSZXRNc2ciOm51bGwsImxvZ2luTWVzc2FnZUVycm9yIjpudWxsLCJsb2dpbkVycm9yVXNlck5hbWUiOm51bGwsImNoZWNrY29kZSI6bnVsbCwiX2V4cGlyZSI6MTU0NTcyNjIyNjQxOCwiX21heEFnZSI6ODY0MDAwMDB9; ali-ss.sig=HsHEw2wv-r_uANtj0aBPreuwTPlYOL8uRQIhOmRB46E; _sync_time_=1545640533494; _sync_time_.sig=xTBcxRdS8dPBXXO1dkxZzou8Kf7FJ2e5BI1R_MiNVBM; cn_tmp=Z28mC+GqtZ3ZcqjPNSPy4lcdY/PAmJdxn+XXXBhfEhslmMoUe0HbNJgpn5LrmZpaSfjweXS8sRfxLhSkPV6Mqrjh4I5Clm7891Oqx6rRJ9cT/eirE48/qQ==; cn_tmp.sig=JDVDZEn7qRcpk8PbMZOoPSpzHE5AWhTFZHTmg5OXCn4; webp=1; _is_show_loginId_change_block_=b2b-2439417348_false; _show_force_unbind_div_=b2b-2439417348_false; _show_sys_unbind_div_=b2b-2439417348_false; _show_user_unbind_div_=b2b-2439417348_false; __rn_alert__=false; csg=8b8dc92f; ali_apache_track=c_mid=b2b-2439417348|c_lid=%E6%80%AA%E5%8F%AB%E4%B9%8B%E9%9F%B3|c_ms=1; alicnweb=touch_tb_at%3D1545641195181%7Clastlogonid%3D%25E6%2580%25AA%25E5%258F%25AB%25E4%25B9%258B%25E9%259F%25B3%7Chp_newbuyerguide%3Dtrue%7Cshow_inter_tips%3Dfalse%7ChomeIdttS%3D62375374954442716969013518078279867570%7ChomeIdttSAction%3Dtrue; CNZZDATA1253659577=1741694258-1545614360-%7C1545641360; __cn_logon__=false; isg=BMXFMBGJNWSsURRWNzuhjAsh1AE_KnmorL4U_8cqgfwLXuXQj9KJ5FM_bMINHpHM; l=aBCbLOoTysOjMCsXSMa50lbNq707y1BzDHnY1MaHlTEhNPF5DjCL1jno-VwW7_qC55Ty_K-5F",
    'dnt': "1",
    'upgrade-insecure-requests': "1",
    'user-agent': "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
    }
headers= {
    'Host': 'm.1688.com',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'zh-CN,zh;q=0.8,en;q=0.6',
'cookie': 'UM_distinctid=15e6ed6dd7b2e-06c073d5616d11-701238-13c680-15e6ed6ddae5fc; JSESSIONID=8L788vtu1-0nHYM04zOc0TQfmxv7-wbFgsUQ-h8rA; _csrf_token=1505119984929; ctoken=VqjT3BORsax2iWjCAgsenaga; h_keys="%u7ae5%u88c5#%u5973%u88c5"; _m_h5_tk=bb17c380672e4d95e808bb534164b7c0_1505196304800; _m_h5_tk_enc=2cfa522141250a3bd9c33d63b6f51f0e; ad_prefer="2017/09/12 13:28:07"; ali_ab=113.140.248.6.1505098102710.5; CNZZDATA1261998348=1859853114-1505182966-%7C1505192053; ali-ss=eyJ1c2VySWQiOm51bGwsImxvZ2luSWQiOm51bGwsInNpZCI6bnVsbCwiZWNvZGUiOm51bGwsIm1lbWJlcklkIjpudWxsLCJzZWNyZXQiOiJXRzhSdVNKWjNFLWk3anNCLXdDaGhoZHIiLCJfZXhwaXJlIjoxNTA1MjgxMTI3NzU4LCJfbWF4QWdlIjo4NjQwMDAwMH0=; _tmp_ck_0="pgW8OZwgGvg%2FDIoFQVXmLDxmZ1BWDCaddlGOdWaOT2rvg4Up3hA%2Fw%2B99PkpLnoRjM8LJijQy%2BOzlmcIY1bxgbPsFeiyH9gz6D9nI7AiBhWXl38%2F%2F%2Bt91x9IQHD26CA9DShonSItFtED2F15sxFWlrpRY8ctSNgjHEp1Bqhm2fUwJccgs9Wq7G76KbtL892fFD9WaYAixodxWqxJxQv7TSjmemwsMIGF5yNplRhpK9ImFYU2hjDh9fv2hI%2BHYuUG%2BmvZHREL1%2BN6B%2Fejy5Op1rf3RiU%2Fj6kBAfxVxyQ2bTo%2F83jYm9jPw7Uxnb9wbeDqQQQChsQnW1Q5726RySiuESfZomfwtJ5jS"; __cn_logon__=false; cna=TZvcEaNlqAECAXGM++5Tq7Jv; webp=1; alicnweb=touch_tb_at%3D1505199489018; isg=AjY2XflYczZ7rwdQKRBlXZ65h2z4_3v5Z904uaAbw5ld49d9COZQoGLRjYl0',
'referer': 'https://m.1688.com/offer_search/-CDAFD7B0.html?spm=a26g8.7662790.0.0.3t5XoC',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.0'
    }
#https://m.1688.com/offer/555557089718.html?spm=a2615.2177701.0.0.11044791ulTpSj#offer-template-1
url1='https://m.1688.com/offer/576809723450.html?spm=a2615.7691456.newlist.88.7b02eb93VyPf0u/'
url2='https://m.1688.com/offer/577266390102.html?spm=a2615.7691456.autotrace-offerGeneral.44.5ce226a0JFHV0p'

url3='https://detail.1688.com/offer/577266390102.html'
url4='https://m.1688.com/offer/44595212707.html?spm=a2615.7691456.0.0.4ea79626S06oAo'
def proxy_check(pro='', url=url3, t=1):
    # print t
    for i in range(t):
        try:
            # print proxie
            # pro={}
            htt = 'http://' + pro.strip()
            print htt
            pros = {"https": htt}
            # print pro
            # proxie.decode('utf-8').encode('utf-8')
            req = requests.get(url, proxies=pros, timeout=5, headers=headers)
            # print 'buf'
            if req.status_code == 200:
                # print 'fule'
                print req.status_code
                # ccc='<a id="a-link" href="https://www.taobao.com/markets/bx/deny_pc"></a> <script>document.getElementById("a-link").click();</script>'
                #
                #print req.content
                print bytes(bytearray(req.text, encoding='utf-8'))
                #print req.content,type(req.content),bytes(bytearray(req.text, encoding='utf-8'))
                # print req.content.__repr__(),ccc.__repr__()
                # print req.content.__str__(),ccc.__str__()
                # print len(req.content), len(ccc)
                # print dir(req.content)
                # print dir('<a id="a-link" href="https://www.taobao.com/markets/bx/deny_pc"></a> <script>document.getElementById("a-link").click();</script>')

                if len(req.content)<=500:




                    print '内容错了'
                    if i == t - 1:
                        return False
                else:
                    pass
                #  print req.content&toSite=main
                # print type(req.content)
                # req.encoding='gbk'
                # print req.text
                # print type(req.text)
                # reqq = bytes(bytearray(req.text, encoding='utf-8'))
                # print reqq
                # print type(reqq)

                return True
            else:
                print req.status_code

                if i == t - 1:
                    return False
        except Exception, ex:
            # print 'check'
            print Exception, ":", ex
            if i == t - 1:
                return False
#获取免费代理
def request1():
    url = "http://154.48.238.17:5080/test"
    try:

        f = requests.get(url)
    except Exception as e:
        print 'proxy ip error!' ,e
        return False

    #print type(f.content)
    return eval(f.content)
def url_check():
    req = request1()
    for r in req:
        print proxy_check(pro=r)


if __name__ == '__main__':
   # print request1()
    print proxy_check(pro='123.161.22.182:9797',t=2)
    url_check()

#47.105.84.240:80
#47.105.138.112:80
